package com.hnluchuan.staff.dao;

import com.hnluchuan.core.dao.BaseDAO;
import com.hnluchuan.core.support.Page;
import com.hnluchuan.staff.dto.EmployeeFileDTO;
import com.hnluchuan.staff.model.EmployeeFile;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
@Repository
public class EmployeeFileDAO extends BaseDAO<EmployeeFile> {
    public List<EmployeeFile> find(EmployeeFileDTO dto, Page page) {
        StringBuilder hql = new StringBuilder();
        List<Object> paras = new ArrayList<Object>();
        hql.append("From EmployeeFile n where 1 = 1 ");
        if (dto.getId() != null) {
            hql.append(" and n.id = ? ");
            paras.add(dto.getId());
        }
        if (dto.getEmployee() != null) {
            if (dto.getEmployee().getId() != null) {
                hql.append(" and n.employee.id = ? ");
                paras.add(dto.getEmployee().getId());
            }
        }
        if (StringUtils.isNotBlank(dto.getFiledescribe())) {
            hql.append(" and n.filedescribe like ? ");
            paras.add("%" + dto.getFiledescribe() + "%");
        }
        if (StringUtils.isNotBlank(dto.getSavepath())) {
            hql.append(" and n.savepath = ? ");
            paras.add(dto.getSavepath());
        }
        hql.append(" order by n.id desc ");
        return super.find(hql.toString(), paras.toArray(), page);
    }


    public EmployeeFile findOne(EmployeeFileDTO dto) {
        Page page = new Page();
        page.setPageSize(1);
        page.setPage(1);
        List<EmployeeFile> list = this.find(dto, page);
        return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }

    @Override
    public Long create(EmployeeFile employeeFile) {
        return super.create(employeeFile);
    }

    @Override
    public void update(EmployeeFile employeeFile) {
        super.update(employeeFile);
    }

}
